package com.theobserver.theobserver;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.GetCallback;

import java.util.ArrayList;

/**
 * Created by wssxk on 7/11/17.
 */

public class NewsSendedActivity extends Activity {


    //Context mContext = null;
    AVUser cur_user = null;
    SwipeRefreshLayout sl = null;
    NewsListAdapter adapter = null;

    public void onCreate(Bundle savedInstance)
    {
        //View v = inflater.inflate(R.layout.news_list_without_catalog_main_page, null);
        super.onCreate(savedInstance);
        setContentView(R.layout.news_list_without_catalog_independ_page);

        TextView newssended_title = (TextView) findViewById(R.id.textview_withoutca_independ_title);
        newssended_title.setText("我发布的新闻");
        View add_more = findViewById(R.id.relativelayout_news_addmore);
        add_more.setVisibility(View.GONE);

        ListView lv = (ListView) findViewById(R.id.listview_news);
        adapter = new NewsListAdapter(NewsSendedActivity.this);
        lv.setAdapter(adapter);

        sl = (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout_news);
        sl.setRefreshing(true);

        ImageView imageview_star = findViewById(R.id.imageview_column_detail_star);
        imageview_star.setVisibility(View.GONE);

        ImageView imageview_addnews = findViewById(R.id.imageview_sendednews_add);
        imageview_addnews.setVisibility(View.VISIBLE);
        imageview_addnews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(NewsSendedActivity.this, SendNewsActivity.class), Code.SENDNEWSACTIVITY);
            }
        });

        TextView textview_return = findViewById(R.id.textview_newslist_independ_return);
        textview_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //用户要改
        /*
        cur_user = new AVUser();
        cur_user.setObjectId("5961b956ac502e005819946c");
        cur_user.fetchInBackground(new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                if(e == null)
                {
                    News.getSendedNews(cur_user, new NewsCallBack() {
                        @Override
                        public void getNews(ArrayList<News> news, int state, AVException e) {

                            sl.setRefreshing(false);
                            if(news != null && news.size() != 0)
                            {

                                adapter.data.clear();
                                adapter.data.addAll(news);
                                adapter.notifyDataSetChanged();
                            }
                            else
                            {
                                adapter.data.clear();
                                adapter.notifyDataSetChanged();
                            }
                        }
                    });

                }
            }
        });
        */

        cur_user = AVUser.getCurrentUser();

        News.getSendedNews(cur_user, new NewsCallBack() {
            @Override
            public void getNews(ArrayList<News> news, int state, AVException e) {

                sl.setRefreshing(false);
                if(news != null && news.size() != 0)
                {

                    adapter.data.clear();
                    adapter.data.addAll(news);
                    adapter.notifyDataSetChanged();
                }
                else
                {
                    adapter.data.clear();
                    adapter.notifyDataSetChanged();
                }
            }
        });

        sl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                News.getSendedNews(cur_user, new NewsCallBack() {
                    @Override
                    public void getNews(ArrayList<News> news, int state, AVException e) {
                        sl.setRefreshing(false);
                        if(news != null && news.size() != 0)
                        {
                            adapter.data.clear();
                            adapter.data.addAll(news);
                            adapter.notifyDataSetChanged();
                        }
                        else
                        {
                            adapter.data.clear();
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent news_detail_intent = new Intent(NewsSendedActivity.this, NewsDetailActivity.class);
                //news_detail_intent.putExtras();
                news_detail_intent.putExtra("objectId",
                        ((NewsItemFrameLayout) view.findViewById(R.id.framelayout_news_item)).news.cur_news.getObjectId());
                startActivity(news_detail_intent);
            }
        });

    }


    public void onActivityResult(int requestCode,int ResultCode,Intent data) {
        switch (requestCode)
        {
            case Code.SENDNEWSACTIVITY:
                sl.setRefreshing(true);
                News.getSendedNews(cur_user, new NewsCallBack() {
                    @Override
                    public void getNews(ArrayList<News> news, int state, AVException e) {

                        sl.setRefreshing(false);
                        if(news != null && news.size() != 0)
                        {

                            adapter.data.clear();
                            adapter.data.addAll(news);
                            adapter.notifyDataSetChanged();
                        }
                        else
                        {
                            adapter.data.clear();
                            adapter.notifyDataSetChanged();
                        }
                    }
                });

                break;
        }
    }



}
